//
//  QJNetworkManager.m
//  QuanJieShopProject
//
//  Created by apple on 2018/1/30.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJNetworkManager.h"

@implementation QJNetworkManager
#pragma mark -- 初始化
- (instancetype)init{
    self = [super init];
    if (self) {
        //设置基础网络请求地址
        self.manager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:NET_BASEURL]];
        AFHTTPRequestSerializer *requestSerializer =  [AFHTTPRequestSerializer serializer];
        //请求格式
        _manager.requestSerializer = requestSerializer;
        //返回数据格式
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        //支持的格式 @"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain"
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain", @"text/xml", @"json/json", @"image/jpeg", @"image/png",nil];
        
//        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; // 设置content-Type为text/html
//        manager.requestSerializer = [AFJSONRequestSerializer serializer];
//        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        
        //数据请求的时候状态栏是否有菊花的动画
        [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    }
    return self;
}
#pragma mark -- 请求数据结果处理
- (void)requestResponse:(id)response operation:(NSURLSessionDataTask *)operation success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, id))failure{
    
    NSLog(@"response = %@", response);
    NSLog(@"error = %@", operation.error);
    //错误编码
    NSError * _Nullable error = operation.error;
    if (error) {
        
        if (error.code == 3840) {
            failure(operation,[NSDictionary dictionaryWithObjects:@[@"3840",@"数据格式错误!"] forKeys:@[@"code",@"msg"]]);
            return;
        }else if (error.code == -1004){
            failure(operation,[NSDictionary dictionaryWithObjects:@[@"-1004",@"无法连接到服务器!"] forKeys:@[@"code",@"msg"]]);
            return;
        }else{
            NSString *code = [NSString stringWithFormat:@"%ld",(long)error.code];
            failure(operation,[NSDictionary dictionaryWithObjects:@[code, error.localizedDescription] forKeys:@[@"code", @"msg"]]);
            return;
        }
        
    }else{
        
//        KMyLog(@"response = %@", response);
//        KMyLog(@"response.class = %@", [response class]);

        if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
            
            // 500, 5开头的服务器问题
            // 404, 4开头的请求错误
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
//            KMyLog(@"httpResponse = %zd", httpResponse.statusCode);

            //*
            // error 后天可能会返回 null, 这里判断下
            if (failure) {
                NSDictionary *dict = [[NSDictionary alloc] init];
                // 这里用字典类型, 是因为HttpManager里的id类型直接调用了字典方法, 避免错误
                failure(operation,dict);
                return;
            }
            return;
            //*/
        }
        
        if ([response isKindOfClass:[NSDictionary class]]) {
            
            NSNumber *num = [response objectForKey:@"status"];
            if (num.intValue == 1) { // 请求成功
                if (success) {
                    success(operation,response[@"data"]);
                    return;
                }
            }else if (num.intValue == -100) { // token失效
                //需要重新登录了,清除当前的用户信息
                [QJUserInfo RemoveCurrentUser];
                
                //弹出登录界面
                [[NSNotificationCenter defaultCenter] postNotificationName:@"alertLoginView" object:nil];
                if (failure) {
                    failure(operation,response[@"data"]);
                    return;
                }
            }else { // 其他所有情况
                if (failure) {
                    failure(operation,response[@"data"]);
                    return;
                }
            }
            
            
//            //数据请求成功
//            NSDictionary *responseDic = (NSDictionary *)response;
//            if ([[responseDic objectForKey:@"code"] integerValue] == 200) {
//                if (success) {
//                    success(operation,[responseDic objectForKey:@"data"]);
//                    return;
//                }
//            }else if ([[responseDic objectForKey:@"code"] integerValue] == 10001){
//                //需要重新登录了
//                //清除当前的用户信息
//                [QJUserInfo RemoveCurrentUser];
//
//                NSLog(@"======弹框  发出通知");
//                //弹出登录界面
//                [[NSNotificationCenter defaultCenter] postNotificationName:@"alertLoginView" object:nil];
//                if (failure) {
//                    failure(operation,responseDic);
//                    return;
//                }
//            }else{
//                if (failure) {
//                    failure(operation,responseDic);
//                    return;
//                }
//            }
        }

    }
}
@end
